package com.lingtianyu.tech.springcloud.grayzone.grey.interceptor;


import com.lingtianyu.tech.springcloud.grayzone.grey.context.GreyContext;
import com.lingtianyu.tech.springcloud.grayzone.grey.loadbalancer.GreyLoadBalancerClient;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

public class GreyRequestInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        String springCloudZone = GreyContext.getSpringCloudZone();
        if (springCloudZone != null && !springCloudZone.isEmpty()) {
            request.getHeaders().set(GreyLoadBalancerClient.ZONE_NAME, springCloudZone);
        }
        return clientHttpRequestExecution.execute(request, body);
    }

//    @Override
//    public void apply( requestTemplate) {
//        //TODO  考虑添加拦截器,并在threadlocal中设置值,拦截器若没有,就清空
//        // 设置完成后在这里set,然后在负载均衡那里,根据threadlocal中的值,做灰度负载
//        // 后续再继续加入 skywalking的支持
//        // 考虑添加gateway做统一网关

//    }
}
